//
//  ActivityListViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ActivityDetailsViewController.h"
#import "ActivityListViewController.h"
#import "ActivityTableViewCell.h"
#import "Activity.h"
#import "UIImageView+WebCache.h"
#import "netWorkHandle.h"
#import <sqlite3.h>

@interface ActivityListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *actArr;
@property (nonatomic, retain) UITableView *tableV;

@end

@implementation ActivityListViewController

- (void)viewDidLoad {


    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
  /********************************** 活动列表界面 *************************************/
    
    
    self.navigationItem.title = @"活动";
    
    [self getData];
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 120) style: UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    // 去掉cell间的分割线
    self.tableV.separatorStyle = NO;
    
    [self.view addSubview:self.tableV];
    [self.tableV release];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    while (self.actArr.count == 0) {
        [self.tableV reloadData];
    }
}

// 获取并解析数据
- (void)getData {
    
    self.actArr = [NSMutableArray array];
    
    [netWorkHandle getDataUnSyWithUrlStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" block:^(NSData *netData) {
        

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:netData options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dic objectForKey:@"events"];
        
        for (NSDictionary *tempDic in arr) {
            
            Activity *act = [[Activity alloc] init];
            [act setValuesForKeysWithDictionary:tempDic];
            
            act.name = [[tempDic objectForKey:@"owner"] objectForKey:@"name"];
            act.image = [tempDic objectForKey:@"image"];
            [self.actArr addObject:act];
            [act release];
        }
        
        [self.tableV reloadData];
    }];
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.actArr count];
}

// 行间距
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 180;
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellStr = @"cell";
    
    NSRange range = {5, 11};
    
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    
    if (nil == cell) {
        
        cell = [[ActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
    }
    
    Activity *act = [[Activity alloc] init];
    act = [self.actArr objectAtIndex:indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 活动标题
    cell.nameLabel.text = act.title;
    // 活动图片
    [cell.imageV sd_setImageWithURLStr:act.image];
    // 活动时间
    NSString *time = [NSString stringWithFormat:@"%@ -- %@", [act.begin_time substringWithRange:range], [act.end_time substringWithRange:range]];
    cell.timeLabel.text = time;
    // 活动地址
    cell.addressLabel.text = act.address;
    // 活动类型
    cell.typeressLabel.text = [NSString stringWithFormat:@"类型:  %@", act.category_name];
    // 感兴趣
    cell.interestLabel.text = [NSString stringWithFormat:@"%@", act.wisher_count];
    // 参加
    cell.joinLabel.text = [NSString stringWithFormat:@"%@", act.participant_count];
    
    return cell;
}


// 进入详情界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ActivityDetailsViewController *activeityDetailsVC = [[ActivityDetailsViewController alloc] init];
    Activity *act = [self.actArr objectAtIndex:indexPath.row];
    
    // 传值
    activeityDetailsVC.act = act;
    
    [self.navigationController pushViewController:activeityDetailsVC animated:YES];
    [activeityDetailsVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
 
}


@end
